%%%---------------------------------------------------------------------------------------
%%% @author     Stuart Jackson <simpleenigma@gmail.com> [http://erlsoft.org]
%%% @copyright  2006 - 2007 Simple Enigma, Inc. All Rights Reserved.
%%% @doc        ErlWeb
%%% @reference  See <a href="http://erlsoft.org/modules/erlweb" target="_top">Erlang Software Framework</a> for more information
%%% @reference See <a href="http://erlweb.googlecode.com" target="_top">ErlWeb Google Code Repository</a> for more information
%%% @version    0.0.2
%%% @since      0.0.1
%%% @end
%%%
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2007 Stuart Jackson, Simple Enigma, Inc. All Righs Reserved
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%%
%%%---------------------------------------------------------------------------------------
-module(erlweb).
-author('sjackson@simpleenigma.com').

-export([start/0,unload/0,load/0,restart/0,stop/0,reload/0]).

start() -> 
	io:format("Starting ~p Modules ...~n",[?MODULE]),
	application:start(?MODULE).

load() -> 
	io:format("Loading ~p Modules ...~n",[?MODULE]),
	application:load(?MODULE).

unload() -> 
	io:format("Unloading ~p Modules ...~n",[?MODULE]),
	application:unload(?MODULE).

stop() ->
	io:format("Stopping ~p Modules ...~n",[?MODULE]),
	application:stop(?MODULE).

restart() -> 
	stop(),
	unload(),
	load(),
	reload(),
	start().

check() -> lists:keymember(?MODULE,1,application:loaded_applications()).

check_load() ->
	case check() of
		true -> ok;
		false -> load()
	end.


%%-------------------------------------------------------------------------
%% @spec () -> any()
%% @doc Reloads modules
%% @end
%%-------------------------------------------------------------------------
reload() ->
	check_load(),
	io:format("Reloading ~p code Modules ...~n",[?MODULE]),
	case application:get_key(?MODULE,modules) of
		{ok,Modules} -> 
			code_reload(lists:delete(?MODULE,Modules));
		{error,Reason} -> {error,Reason}
	end.
	
code_reload(Modules) when is_list(Modules) ->
	code_reload(lists:sort(Modules),quiet).

code_reload(Module,_Status) when is_atom(Module) ->
	code:purge(Module),
	code:delete(Module),
	code:load_file(Module),
	true;
code_reload([H|T],Status) ->
	if 
		Status == quiet -> ok;
		Status == verbose -> io:format("Reloading ~w ...~n",[H])
	end,
	code_reload(H,Status),
	code_reload(H,Status),
	code_reload(T,Status);
code_reload([],_Status) -> true.